<?php

/**
 *	  资金流向：支出
 *    支出类型：转账
 *
 *    @author   psmb
 *    @usage    none
 */
class TransferOutlay extends OutlayDepopay
{
    var $_purpose_name = 'transfer';
	
	
	function submit($data)
	{
		/* 释放trade_info和extra_info和post三个变量 */
        extract($data);
        /* 处理交易基本信息 */
        $base_info = $this->_handle_trade_info($trade_info, $post);
        if (!$base_info)
        {
            /* 基本信息验证不通过 */
            return false;
        }
		
		$tradesn = $this->_gen_trade_sn();
		
		/* 开始插入收支记录 */
		if(!$this->_insert_record_info($tradesn, $trade_info)) {
			return false;
		}
		
		/* 如果有转账手续费，则扣除转出账户的手续费 */
		if($transfer_rate = $this->_get_deposit_setting($trade_info['user_id'], 'transfer_rate')) {
			if(!parent::$this->_sys_chargeback($tradesn, $trade_info, $transfer_rate, 'transfer_fee')) {
				$this->_errorCode[] = "50015";
				return false;
			}
		}
					
		return $tradesn;
	}
	
	/* 插入收支记录，并变更账户余额 */
	function _insert_record_info($tradesn, $trade_info)
	{
		$time = gmtime();
		
		// 转出的账户
		$data_record = array(
			'tradesn'	=>	$tradesn,
			'user_id'	=>	$trade_info['user_id'],
			'party_id'	=>	$trade_info['party_id'],
			'amount'	=>	$trade_info['amount'],
			'balance'	=>	$this->_get_deposit_balance($trade_info['user_id'], $trade_info['amount']), // 扣除后的余额
			'flow'		=>	$this->_flow_name,
			'purpose'	=>	$this->_purpose_name,
			'status'	=>	'SUCCESS',
			'payway'	=>	LANG::get('deposit'),
			'name'		=>	LANG::get(strtolower($this->_purpose_name)),
			'remark'	=>	'',
			'add_time'	=>	$time,
			'pay_time'	=>	$time,
			'end_time'	=>	$time,
		);
		$step1 = parent::_insert_deposit_record($data_record);
		
		if($step1)
		{
			/* 转入的账户 */
			$data_record['user_id']		= 	$trade_info['party_id'];
			$data_record['party_id'] 	=	$trade_info['user_id'];
			$data_record['balance']		=	$this->_get_deposit_balance($trade_info['party_id']) + $trade_info['amount']; // 增加后的余额
			$data_record['flow']		=	'income';

			$step2 = parent::_insert_deposit_record($data_record);
			
			if(!$step2) {
				$this->_errorCode[] = "50012";
				return false;
			}
			
			return true;
		}
		else
		{
			$this->_errorCode[] = "50011";
			return false;
		}
	}
}

?>
